﻿using System;
using YmtAuth.Domain.Model.BlacklistUser;
using YmtAuth.Domain.Model.LoginAuth;
using YmtAuth.Domain.Repository;
using YmtAuth.Domain.Shard;
using YmtSystem.CrossCutting;

namespace YmtAuth.AppService
{
    internal class UserStatesAppService
    {
        private static readonly IUserLoginBillsRepository UlcRepo = LocalServiceLocator.GetService<IUserLoginBillsRepository>();
        private static readonly IYmatouBlacklistUserRepository BlacklistUsrRepo = LocalServiceLocator.GetService<IYmatouBlacklistUserRepository>();
        /// <summary>
        /// 签名是否启用检查用户被锁定功能
        /// </summary>
        /// <returns></returns>
        public static bool EnableSignTokenCheckUserLocked()
        {
            var cfg = System.Configuration.ConfigurationManager.AppSettings["_SignTokenCheckUserLocked"];
            if (cfg.IsEmpty()) return false;
            //if (cfg == "1") return true;
            //return false;
            return cfg == "1";
        }
        /// <summary>
        /// 获取token是否启用检查用户被锁定功能
        /// </summary>
        /// <returns></returns>
        public static bool EnableGetTokenCheckUserLocked()
        {
            var cfg = System.Configuration.ConfigurationManager.AppSettings["_GetTokenCheckUserLocked"];
            if (cfg.IsEmpty()) return false;
            //if (cfg == "1") return true;
            //return false;
            return cfg == "1";
        }
        /// <summary>
        /// 检查用户是否被锁定(黑名单用户也被视为锁定）
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static UserStates CheckUserLocked(string loginId)
        {
            UserStates states = UserStates.Normal;
            if (YmatouBlacklistUser.Enable)
            {
                states = CheckBlacklistUser(loginId).Item1;
                if (states != UserStates.Normal)
                    return UserStates.BlacklistUser;
            }
            states = OnlyCheckUserLocked(loginId);
            return states;
        }
        /// <summary>
        /// 检查是否为黑名单用户
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static Tuple<UserStates, string> CheckBlacklistUser(string loginId)
        {
            if (!YmatouBlacklistUser.Enable) return Tuple.Create(UserStates.Normal, "ok");

            var blackUser = BlacklistUsrRepo.FindOne(YmatouBlacklistUserSpecifications.MatchUser(loginId));
            if (blackUser == null) return Tuple.Create(UserStates.Normal, "ok");

            return blackUser.IsNotAllowLogin() ? Tuple.Create(UserStates.BlacklistUser, blackUser.ResponseMsg) : Tuple.Create(UserStates.Normal, "ok");
        }
        /// <summary>
        /// 只检查用户是否被锁定（不查询黑名单）
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static UserStates OnlyCheckUserLocked(string loginId)
        {
            var userInfo = UlcRepo.FindOne(UserLoginAuthSpecifications.MatchLoginId(loginId), true);
            YmtSystemAssert.AssertArgumentNotNull(userInfo, string.Format("用户{0}不存在", loginId));
            return userInfo.IsLocked() ? UserStates.Locked : UserStates.Normal;
        }
        /// <summary>
        /// 检查用户是否被锁定（黑名单用户也被视为锁定）
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static UserStates CheckUserLocked(int userId)
        {
            var userInfo = UlcRepo.FindOne(UserLoginAuthSpecifications.MatchUserId(userId), true);
            YmtSystemAssert.AssertArgumentNotNull(userInfo, string.Format("用户{0}不存在", userId));
            if (userInfo.IsLocked()) return UserStates.Locked;
            var blackUser = BlacklistUsrRepo.FindOne(YmatouBlacklistUserSpecifications.MatchUser(userInfo.LoginBills.LoginId));
            if (blackUser == null) return UserStates.Normal;
            if (blackUser.IsNotAllowLogin()) return UserStates.BlacklistUser;
            return UserStates.Normal;
        }

        /// <summary>
        /// 检查用户是否被锁定（黑名单用户也被视为锁定）
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static UserStates CheckUserLocked2(int userId)
        {
            var userInfo = UlcRepo.FindOne(UserLoginAuthSpecifications.MatchUserId(userId), true);

            //WESLEY-20170213: 兼容关闭双“写”
            if (userInfo == null) return UserStates.Normal;

            if (userInfo.IsLocked()) return UserStates.Locked;
            var blackUser = BlacklistUsrRepo.FindOne(YmatouBlacklistUserSpecifications.MatchUser(userInfo.LoginBills.LoginId));
            if (blackUser == null) return UserStates.Normal;
            if (blackUser.IsNotAllowLogin()) return UserStates.BlacklistUser;
            return UserStates.Normal;
        }
    }
}
